package com.zenithmind.library.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 图书视图对象
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Data
@Schema(name = "图书视图对象", description = "图书信息的视图对象")
public class BookVO {

    @Schema(description = "图书ID")
    private String id;

    @Schema(description = "图书标题")
    private String title;

    @Schema(description = "副标题")
    private String subtitle;

    @Schema(description = "ISBN")
    private String isbn;

    @Schema(description = "ISSN")
    private String issn;

    @Schema(description = "分类ID")
    private String categoryId;

    @Schema(description = "分类名称")
    private String categoryName;

    @Schema(description = "出版社ID")
    private String publisherId;

    @Schema(description = "出版社名称")
    private String publisherName;

    @Schema(description = "作者IDs")
    private String authorIds;

    @Schema(description = "作者姓名")
    private String authorNames;

    @Schema(description = "出版日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate publishDate;

    @Schema(description = "版本")
    private String edition;

    @Schema(description = "页数")
    private Integer pages;

    @Schema(description = "语言")
    private String language;

    @Schema(description = "图书简介")
    private String summary;

    @Schema(description = "目录")
    private String tableOfContents;

    @Schema(description = "封面图片URL")
    private String coverImage;

    @Schema(description = "价格")
    private BigDecimal price;

    @Schema(description = "关键词")
    private String keywords;

    @Schema(description = "标签")
    private String tags;

    @Schema(description = "图书类型")
    private Integer bookType;

    @Schema(description = "图书类型名称")
    private String bookTypeName;

    @Schema(description = "总库存数量")
    private Integer totalCopies;

    @Schema(description = "可借阅数量")
    private Integer availableCopies;

    @Schema(description = "已借出数量")
    private Integer borrowedCopies;

    @Schema(description = "总借阅次数")
    private Long totalBorrowCount;

    @Schema(description = "评分")
    private BigDecimal rating;

    @Schema(description = "评价数量")
    private Integer reviewCount;

    @Schema(description = "状态")
    private Integer status;

    @Schema(description = "状态名称")
    private String statusName;

    @Schema(description = "是否推荐")
    private Integer isRecommended;

    @Schema(description = "是否新书")
    private Integer isNew;

    @Schema(description = "是否热门")
    private Integer isPopular;

    @Schema(description = "位置信息")
    private String location;

    @Schema(description = "备注")
    private String remarks;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 获取图书类型名称
     */
    public String getBookTypeName() {
        if (bookType == null) {
            return "未知";
        }
        return switch (bookType) {
            case 0 -> "纸质书";
            case 1 -> "电子书";
            case 2 -> "音频书";
            default -> "未知";
        };
    }

    /**
     * 获取状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "下架";
            case 1 -> "上架";
            default -> "未知";
        };
    }

    /**
     * 判断是否可借阅
     */
    public boolean isAvailable() {
        return status != null && status == 1 && 
               availableCopies != null && availableCopies > 0;
    }

    /**
     * 计算库存利用率
     */
    public double getStockUtilization() {
        if (totalCopies == null || totalCopies == 0) {
            return 0.0;
        }
        int borrowed = borrowedCopies != null ? borrowedCopies : 0;
        return (double) borrowed / totalCopies * 100;
    }
}
